package com.kmwiki.Utility.Net;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URLEncoder;

public class myHttp {
	
	public myHttp(){
	}

	public String getURL(String urlstr, String Encode) {
		String result = "";
		try {
	        URL url = new URL(urlstr);
	        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(),Encode));
	        String line;
	
	        while ((line = reader.readLine()) != null) {
	        	result += line;
	        }
	        reader.close();
		} catch (MalformedURLException e) {
		        // ...
		} catch (IOException e) {
		    // ...
		}
		return result;
	}
	
	public String getHttpURLConnection(String urlstr, String Encode) throws IOException {
		String result = "";
		HttpURLConnection connection = null;
        OutputStreamWriter wr = null;
        BufferedReader rd  = null;
        StringBuilder sb = null;
        String line = null;
    
        URL serverAddress = null;
        try {
	      serverAddress = new URL(urlstr);
		  connection = null;
	
		  //Set up the initial connection
		  connection = (HttpURLConnection)serverAddress.openConnection();
		  connection.setRequestMethod("GET");
		  connection.setDoOutput(true);
		  connection.setReadTimeout(10000);
		  connection.connect();
		
		  //get the output stream writer and write the output to the server
		  //not needed in this example
		  //wr = new OutputStreamWriter(connection.getOutputStream());
		  //wr.write("");
		  //wr.flush();
		
		  //read the result from the server
		  rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		  sb = new StringBuilder();
		
		  while ((line = rd.readLine()) != null)
		  {
		      sb.append(line + '\n');
		  }
		  result = sb.toString();  
   	   }
        catch (MalformedURLException e) {
		      e.printStackTrace();
		}
		  finally
		  {
		      //close the connection, set all objects to null
		      connection.disconnect();
		      rd = null;
		      sb = null;
		      wr = null;
		      connection = null;
		  }		
		 return result;
	}	
}
